package de.bensoft.jrail.client.ui.mainform;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.stage.Modality;
import javafx.stage.Stage;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.log4j.Logger;

import de.bensoft.jrail.client.com.GlobalCommunicator;
import de.bensoft.jrail.client.com.SerialCommunicator;
import de.bensoft.jrail.client.common.FormCreator;
import de.bensoft.jrail.client.common.JavaFXController;
import de.bensoft.jrail.client.ui.common.UIConstants;
import de.bensoft.jrail.client.ui.locoadmin.LocoAdminController;
import de.bensoft.jrail.client.ui.locodriver.LocoDriverController;
import de.bensoft.jrail.client.ui.settings.SettingsController;

@Singleton
@JavaFXController(fxml="MainForm.fxml", css=UIConstants.BASIC_CSS)
public class MainFormController {	
	
	private static Logger LOG = Logger.getLogger(MainFormController.class);
	
	@Inject
	private GlobalCommunicator globalComm;
	
	@Inject
	private FormCreator formCreator;

	public MainFormController() {
		
	}
	// **************************************************************************************
	// FXML
	// **************************************************************************************
	@Inject
	private SerialCommunicator serial;
	
	// **************************************************************************************
	// FXML
	// **************************************************************************************
	@FXML
	private ToggleButton tglTrackPower;
	
	@FXML
	private void onClose(ActionEvent ae) {
		Button b = (Button)ae.getSource();
		Stage s = (Stage)b.getScene().getWindow();
		s.close();
	}
	
	@FXML
	private void adminLocos() {
		showAdminLocoDialog();
	}
	
	@FXML
	private void onNewLocoDriver() {
		newLocoDriver();
	}
	
	@FXML
	private void onSettings() {
		showSettings();
	}
	
	@FXML
	private void initialize() {
		initGUI();
	}
	// **************************************************************************************
	// UI
	// **************************************************************************************
	private void initGUI() {
		
		// Trying to connect
		serial.connect();
		
		// Set trackpower off
		globalComm.commandTrackPowerOff();
			
		bindGUI();
	}
	
	private void bindGUI() {

		tglTrackPower.selectedProperty().bindBidirectional(globalComm.trackPowerProperty());
	}
	
	private void showAdminLocoDialog() {		
		Stage stage = new Stage();
		stage.setScene(formCreator.createScene(LocoAdminController.class).getScene());
		stage.setTitle("Locomotive Administration");
		stage.initModality(Modality.APPLICATION_MODAL);
		stage.show();	
	}
	
	private void newLocoDriver() {	
		Stage stage = new Stage();
		stage.setScene(formCreator.createScene(LocoDriverController.class).getScene());
		stage.setTitle("Locodriver");
		stage.setResizable(false);
		stage.sizeToScene();
		stage.show();	
	}
	
	private void showSettings() {
		Stage stage = new Stage();
		stage.setScene(formCreator.createScene(SettingsController.class).getScene());
		stage.setTitle("Settings");
		stage.setResizable(false);
		stage.sizeToScene();
		stage.initModality(Modality.APPLICATION_MODAL);
		stage.show();
	}
}
